// Save the user's credentials if valid, otherwise prompt them to make changes
function saveOptions() {
	if ($("#username").val() == "username@adsl.xnet.co.nz") {
		alert("Please enter your Xnet username.");
		$("#username").setSelection(0,8);
		return false;
	}
	else if ($("#username").val().length > 0 && $("#password").val().length > 0) {
		if (!strstr($("#username").val(), "@")) {
			alert("Please enter your full Xnet username, eg username@adsl.xnet.co.nz");
			$("#username").focus();
			return false;
		}

		// Credentials look okay, so let's save them. Password becomes an md5 hash
		localStorage.username = $("#username").val();
		localStorage.password = hex_md5($("#password").val());
		chrome.extension.sendRequest("update usage");
	}
	else if ($("#username").val() == localStorage.username && $("#password").val().length == 0) {
		//
	}
	else {
		alert("Please enter your Xnet username and password.");
		return false;
	}

	var customVal = $("#custom_cap").val();
	if (customVal.length) {
		localStorage.custom_cap = parseFloat(customVal) * 1024;
	} else {
		localStorage.custom_cap = 0;
	}

	$('input[type="checkbox"]').each(function(){
		localStorage[$(this).attr("id")] = $(this).prop("checked") == 1 ? 1 : 0;
	});

	localStorage.cap = localStorage.custom_cap_enable == 1 ? parseFloat(localStorage.custom_cap) : parseFloat(localStorage.cap_original ? localStorage.cap_original : 0);

	chrome.extension.sendRequest("update tooltip");
	chrome.extension.sendRequest("update usage");
	window.close();
	return false;
}

$(document).ready(function(){
	
	// Set up some placeholder text and pre-selected text if needed
	if (localStorage.username) {
		$("#username").val(localStorage.username);
	} else {
		$("#username").val("username@adsl.xnet.co.nz").setSelection(0,8);
	}
	if (localStorage.password) {
		$("#password").attr("placeholder", "Click to change");
	}
	
	$('form').bind('submit', saveOptions);

	$("#username").bind("keydown", function(){
		$("#password").attr("placeholder","");
	});

	// If Xnet reported invalid credentials, display a message saying so
	if (localStorage.errstr && localStorage.errstr == "Username or Password Incorrect") {
		$("form").prepend('<br /><span style="color:#e54100"><b>The credentials below are incorrect.</b><br />Please confirm your username and password.</span><br /><br />');
	}
	// Otherwise create link for the XML data file text
	else if (localStorage.password && localStorage.password.length) {
		$("#xmlOption").html('<a href="https://api.xport.co.nz/usage/?username='+localStorage.username+';token='+localStorage.password+'" target="_blank">Xnet XML data file</a>');
	}

	// Tick checkboxes
	$('input[type="checkbox"]').each(function(){
		if (localStorage[$(this).attr("id")] && localStorage[$(this).attr("id")] == 1) {
			$(this).prop("checked",1);
		}
	});

	// Custom cap
	$("#custom_cap").val(parseFloat(localStorage.custom_cap) / 1024);

	$("#custom_cap_enable").bind("change", function(){
		if ($(this).prop("checked")) {
			$("#custom_cap").prop("disabled","");
			$("#customCapDetails").css("opacity",1);
		} else {
			$("#custom_cap").prop("disabled",true);
			$("#customCapDetails").css("opacity",0);
		}
	});
	$("#custom_cap_enable").change();

});